﻿using System;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.Core.Domain.EntityBase;
using ZilLion.Service.WeChatMall.Domain.Entitys.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Requests.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Responses.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Services.Interface;

namespace ZilLion.Service.WeChatMallOss.Controllers
{
    /// <summary>
    ///     微商城后台店铺设置
    /// </summary>
    [Route("Api/WechatMallOss/Good")]
    public class WeChatMallGoodSetController : WechatMallOssControllerBase
    {
        private readonly IGsService _gsService;

        /// <summary>
        ///     构造
        /// </summary>
        /// <param name="gsService"></param>
        public WeChatMallGoodSetController(IGsService gsService)
        {
            _gsService = gsService;
        }


        /// <summary>
        ///     获取当前公司的分组
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetGroups")]
        [HttpGet]
        public InputGroupListResponse GetGroups(string token = null)
        {
            var list = _gsService.GetGroupList(GetZlCompanyKey());
            return new InputGroupListResponse {Groups = list};
        }

        /// <summary>
        ///     保存分组信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SaveGroup")]
        [HttpPost]
        public InputGroupResponse SaveGroup([FromBody]InputGroupRequest request, string token = null)
        {
            var entity = request.AutoConvert<GsGroupEntity>();
            entity.ZlCompanyKey = GetZlCompanyKey();
            entity.UpdatedTime = DateTime.Now.GetNowChinaTime();
            entity.Updater = GetUserId();
            var savedentity = _gsService.SaveGroup(entity);
            return new InputGroupResponse {Group = savedentity};
        }

        /// <summary>
        ///     获取单个分组信息
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetGroup")]
        [HttpGet]
        public InputGroupResponse GetGroup(long groupid, string token = null)
        {
            return new InputGroupResponse {Group = _gsService.GetGroupById(groupid) ?? new GsGroupEntity()};
        }

        /// <summary>
        ///     移除输入分组
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("RemoveGroup")]
        [HttpDelete]
        public ResponseBase RemoveGroup(long groupid, string token = null)
        {
            _gsService.RemoveGroup(groupid);
            return new ResponseBase();
        }

        /// <summary>
        ///     保存货品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SaveGood")]
        [HttpPost]
        public GoodSetResponse SaveGood([FromBody]GoodRequest request, string token = null)
        {
            var entity = request.AutoConvert<GsBasicInfoEntity>();
            entity.UpdatedTime = DateTime.Now.GetNowChinaTime();
            entity.Updater = GetUserId();
            entity.ZlCompanyKey = GetZlCompanyKey();
            var relation = new GsGroupRelationShipEntity {GroupId = request.Groupid};
            var dto = _gsService.SaveBasicGsInfo(entity, relation);
            return new GoodSetResponse {Good = dto};
        }

        /// <summary>
        ///     删除货品
        /// </summary>
        /// <param name="gsid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("DeleteGood")]
        [HttpDelete]
        public ResponseBase DeleteGood(long gsid, string token = null)
        {
            _gsService.DeleteGood(gsid);
            return new ResponseBase();
        }

        /// <summary>
        ///     获取货品信息
        /// </summary>
        /// <param name="gsid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetGood")]
        [HttpGet]
        public GoodSetResponse GetGood(long gsid, string token = null)
        {
            var dto = _gsService.GetGsInfoDto(gsid);
            return new GoodSetResponse {Good = dto};
        }

        /// <summary>
        ///     根据分组ID 获取分组下货品
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetGoodsByGroup")]
        [HttpGet]
        public GoodSetListResponse GetGoodsByGroup(long groupid, string token = null)
        {
            var list = _gsService.GetGoodsByGroup(groupid);
            return new GoodSetListResponse {Goods = list};
        }

        /// <summary>
        ///     获取所有货品
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetAllGoods")]
        [HttpGet]
        public GoodSetListResponse GetAllGoods(string token = null)
        {
            var list = _gsService.GetAllGoods();
            return new GoodSetListResponse {Goods = list};
        }

        /// <summary>
        ///     获取app下下属货品信息
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetGoodsByAppid")]
        [HttpGet]
        public GoodSetListResponse GetGoodsByAppid(string appid, string token = null)
        {
            var list = _gsService.GetGoodsByAppid(appid);
            return new GoodSetListResponse {Goods = list};
        }

        /// <summary>
        ///     获取门店下下属货品信息
        /// </summary>
        /// <param name="shopid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetGoodsByShopid")]
        [HttpGet]
        public GoodSetListResponse GetGoodsByShopid(long shopid, string token = null)
        {
            var list = _gsService.GetGoodsByShopid(shopid);
            return new GoodSetListResponse {Goods = list};
        }

        /// <summary>
        ///     获取可以导入的线下货品
        /// </summary>
        /// <returns></returns>
        [Route("GetImportableGs")]
        [HttpGet]
        public OffLineGsResponse GetImportableGs()
        {
            var zlcompnatkey = GetZlCompanyKey();
            var list = _gsService.GetImportableGs(zlcompnatkey);
            return new OffLineGsResponse {List = list};
        }

        /// <summary>
        ///     批量导入线下货品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("ImportOfflineShops")]
        [HttpPost]
        public ResponseBase ImportOfflineGs([FromBody]ImportOfflineGsRequest request)
        {
            var list = request.GsList;
            var groupid = request.GroupId;
            var zlCompanyKey = GetZlCompanyKey();
            var updater = GetUserId();
            _gsService.ImportOfflineGs(list, zlCompanyKey, updater, groupid);
            return new ResponseBase();
        }
    }
}